/*
 * @(#)HapiHttpClient.java
 * Date 2013-03-26
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.hapiclient;

import java.io.IOException;

import ca.uhn.hl7v2.HL7Exception;

import ca.uhn.hl7v2.hoh.api.DecodeException;
import ca.uhn.hl7v2.hoh.api.EncodeException;
import ca.uhn.hl7v2.hoh.api.IReceivable;
import ca.uhn.hl7v2.hoh.api.ISendable;

import ca.uhn.hl7v2.hoh.hapi.api.MessageSendable;

import ca.uhn.hl7v2.hoh.hapi.client.HohClientSimple;

import ca.uhn.hl7v2.model.Message;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-03-26
 * @author         Jim Horner
 */
public class HapiHttpClient extends AbstractHapiClient{

    /** Field description */
    private final HohClientSimple client;


    /**
     * Constructs ...
     *
     *
     * @param config
     */
    public HapiHttpClient(HapiClientConfig config) {

        super(config);
        this.client = new HohClientSimple(config.getUrl());
    }

    /**
     * Method description
     *
     *
     * @param request
     *
     * @return
     */
    public Message sendMessage(Message request) throws HapiClientException {

        Message result = null;

        try {

            ISendable<Message> sendable = new MessageSendable(request);
            IReceivable<Message> receivable =
                this.client.sendAndReceiveMessage(sendable);

            result = receivable.getMessage();

        } catch (HL7Exception | EncodeException | DecodeException
                 | IOException e) {


            throw new HapiClientException("Exception", e);
        }

        return result;
    }
}
